package com.meichis.yndms.repository.manager;

import androidx.lifecycle.MutableLiveData;

import com.google.gson.reflect.TypeToken;
import com.meichis.mcsappframework.utils.MessageTools;
import com.meichis.yndms.http.CallSubscriber;
import com.meichis.yndms.http.IWebServiceCallback;
import com.meichis.yndms.http.RetrofitUtil.RxSchedulersHelper;
import com.meichis.yndms.model.InspectTemplate;
import com.meichis.yndms.model.VstFreeQuestion;
import com.meichis.yndms.repository.ApiUtil;

import java.util.ArrayList;
import java.util.List;

public class VSTFreeQuestionnaireManager extends BaseManager {

    private static VSTFreeQuestionnaireManager INSTANCE;

    public static VSTFreeQuestionnaireManager getInstance() {
        if (INSTANCE == null) {
            synchronized (VSTFreeQuestionnaireManager.class) {
                if (INSTANCE == null) {
                    INSTANCE = new VSTFreeQuestionnaireManager();
                }
            }
        }
        return INSTANCE;
    }

    public void getQuestionList(final MutableLiveData<Boolean> showDialog, MutableLiveData<List<InspectTemplate>> data) {
        showDialog.setValue(true);
        getRootApi().RequestSFA(getToken(), ApiUtil.getRequestPack("", "PBMService.GetMyOwnInspectTemplate"))
                .compose(RxSchedulersHelper.rxSchedulerHelper())
                .subscribe(new CallSubscriber(new TypeToken<List<InspectTemplate>>() {
                }.getType(), new IWebServiceCallback<List<InspectTemplate>>() {
                    @Override
                    public void onSucceed(int sequence, List<InspectTemplate> resultPack, String ReturnInfo, int Return) {
                        showDialog.setValue(false);
                        if (resultPack == null || resultPack.size() == 0) {
                            MessageTools.showShortToast("暂无数据");
                            data.setValue(new ArrayList<>());
                        } else {
                            data.setValue(resultPack);
                        }
                    }

                    @Override
                    public void onFailure(int sequence, int errorCode, String error) {
                        showDialog.setValue(false);
                        MessageTools.showShortToast(error);
                    }
                }));

    }
}
